/*
  Copyright (c) 2012 Igor Chernyshev
  Copyright (c) 2012 Nigel Stewart, NVIDIA Corporation
  All rights reserved.

  Redistribution and use in source and binary forms, with or without modification,
  are permitted provided that the following conditions are met:

    Redistributions of source code must retain the above copyright notice, this
    list of conditions and the following disclaimer.

    Redistributions in binary form must reproduce the above copyright notice,
    this list of conditions and the following disclaimer in the documentation
    and/or other materials provided with the distribution.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef __REGAL_SHARED_PTR_H__
#define __REGAL_SHARED_PTR_H__

#include "RegalUtil.h"

REGAL_GLOBAL_BEGIN

// Optimistically assume that TR1 is available
// - Except for Mac
// - Except for gcc before 4.4.x
// - Except for MS Visual Studio before VC10
// - Except for Android

#ifndef REGAL_NO_TR1
#  ifdef __APPLE__
#    define REGAL_NO_TR1 1
#  else
#    ifdef __ANDROID__
#      define REGAL_NO_TR1 1
#    else
#      ifdef __GNUC__
#        if __GNUC__<4 || (__GNUC__==4 && __GNUC_MINOR__<4)
#          define REGAL_NO_TR1 1
#        endif
#      endif
#      if defined(_MSC_VER) && _MSC_VER<1600
#        define REGAL_NO_TR1 1
#      endif
#    endif
#  endif
#endif

#ifndef REGAL_NO_TR1
#  define REGAL_NO_TR1 0
#endif

// Use TR1, if available

#if !REGAL_NO_TR1
#  ifdef _MSC_VER
#    include <memory>
#  else
#    include <tr1/memory>
#  endif
#endif // !REGAL_NO_TR1

REGAL_GLOBAL_END

REGAL_NAMESPACE_BEGIN

#if !REGAL_NO_TR1
using ::std::tr1::shared_ptr;
#else  // !REGAL_NO_TR1

// A dummy impl of TR1 shared_ptr that does not
// support multi-threading.

template<typename T>
class shared_ptr
{
  public:
    inline shared_ptr() : _value(NULL), _count(NULL) {}

    template<typename U>
    inline explicit shared_ptr(U *value) : _value(value), _count(NULL)
    {
      // Inc-ref
    }

    template<typename U>
    inline shared_ptr(const shared_ptr<U> &other)
    : _value(other._value),
      _count(other._count)
    {
      // Inc-ref

      // Allow compilation but prevent use at runtime.
      RegalAssert(false);
    }

    inline shared_ptr &operator=(T *value)
    {
      // Dec-ref

      // Allow compilation but prevent use at runtime.
      RegalAssert(false);
      _value = value;

      // Inc-ref

      return *this;
    }

    template<typename U>
    inline shared_ptr &operator=(const shared_ptr<U> &other)
    {
      // Dec-ref

      // Allow compilation but prevent use at runtime.
      RegalAssert(false);
      _value = other._value;
      _count = other._count;

      // Inc-ref

      return *this;
    }

    inline void reset(T *value)
    {
      operator=(value);
    }

    inline void reset(shared_ptr<T> &other)
    {
      operator=(other);
    }

    template<typename U>
    inline void reset(U *value)
    {
      operator=(value);
    }

    inline       T &operator*()        { return *_value; }
    inline const T &operator*()  const { return *_value; }

    inline       T *operator->()       { return _value; }
    inline const T *operator->() const { return _value; }

    inline       T *get()              { return _value; }
    inline const T *get()        const { return _value; }

  private:
    T      *_value;
    size_t *_count;
};

#endif

REGAL_NAMESPACE_END

#endif
